﻿/**
 * Created by Administrator on 2017/8/31.
 */
define(['jquery','InputContact','reg'],function ($,InputContact,reg) {
    var ContactEdit = {
        name: 'contact-edit',
        template: ''
        + '<div class="cnntItem">'
        + '    <div class="addContact cnntBox clearfix">'
        + '        <div class="connection_girl" v-show="contact.Gender===1"></div>'
        + '        <div class="connection_boy" v-show="contact.Gender===0"></div>'
        + '        <div class="right-conent">'
        + '            <div class="right-connection-top">'
        + '                <span style="float:left;"><em>姓　名：</em>'
        + '                <input type="text"  v-bind:value="contact.NameText" readonly v-if="isSelf" class="inputNameReadonly" />'
        + '                <input type="text" v-on:input="showGenderOption" v-on:propertychange="showGenderOption" v-model="contact.NameText" v-on:blur="setParentValue" v-else />'
        + '                <h3 class="c-shuru" v-show="showGender">'
        + '                    <em v-on:click="isMale">{{maleText}}</em>'
        + '                    <em v-on:click="isFemale">{{femaleText}}</em>'
        + '                </h3></span>'
        + '                <span class="relationTitle">关系：</span>'
        + '                 <em  class="model-right"  v-if="isSelf">本人</em>'
        + '                 <p class="model-right" v-else >'
        + '                    <i-select v-model="contact.Relation" v-on:on-change="setParentValue">'
        + '                        <i-option v-for="item in relationTypeOption" v-bind:value="item.value" v-bind:key="item.label">'
        + '                            {{item.label }}'
        + '                        </i-option>'
        + '                    </i-select>'
        + '                 </p>'
        + '            </div>'
        + '            <div class="right-connection-btm">'
        + '                <div class=" cnnt_xl cnnt_sj_xl addPhone">'
        + '                    <input-contact v-model="contact.Mobile" v-bind:contactType="defaultType" v-bind:contactTypeText="defaultTypeText" v-on:on-blur="setParentValue"></input-contact>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_zj_xl">'
        + '                    <span><i></i>座机：<input type="text" v-model="contact.Telephone.ContactValue" v-on:blur="setParentValue" /></span>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_wx_xl">'
        + '                    <span><i></i>微信：<input type="text" v-model="contact.WeChat.ContactValue" v-on:blur="setParentValue" /></span>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_qq_xl">'
        + '                    <span><i></i>Q Q：<input type="text" v-model="contact.QQ.ContactValue" v-on:blur="setParentValue" /></span>'
        + '                </div>'
        + '            </div>'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            value: {
                type: Object,
                default: function () {
                    return {
                        NameText: "",
                        RelationText: "",
                        Gender: 0,
                        Mobile: [{
                            Type: 0,
                            TypeText: "手机",
                            IsUsually: true,
                            ContactValue: "",
                        }],
                        Telephone: {
                            Type: 1,
                            TypeText: "座机",
                            IsUsually: true,
                            ContactValue: "",
                        },
                        WeChat: {
                            Type: 2,
                            TypeText: "微信",
                            IsUsually: true,
                            ContactValue: "",
                        },
                        QQ: {
                            Type: 3,
                            TypeText: "QQ",
                            IsUsually: true,
                            ContactValue: "",
                        }
                    };
                }
            },
        },
        data: function () {
            return {
                defaultType: 0,
                defaultTypeText: "手机",
                contact: this.setCurrentValue(this.value),
                showGender: false,
                /*联系方式类型*/
                relationTypeOption: [
                    {
                        value: 0,
                        label: '父母'
                    },
                    {
                        value: 1,
                        label: '子女'
                    },
                    {
                        value: 2,
                        label: '朋友'
                    },
                    {
                        value: 3,
                        label: '伴侣'
                    },
                    {
                        value: 4,
                        label: '其他'
                    },

                ],
            };
        },
        components: {
            InputContact: InputContact,
        },
        computed: {
            femaleText: function () {
                return $.trim(this.value.NameText) + "女士";
            },
            maleText: function () {
                return $.trim(this.value.NameText) + "先生";
            },
            isSelf: function () {
                return this.value.Relation === -1
            }
        },
        mounted: function () {
            var thisEdit = this;
            //监听submitEditContact
            window.bus.$on("submitEditContact", function () {
                thisEdit.setParentValue();
            });
        },
        methods: {
            showGenderOption: function () {
                if ($.trim(this.value.NameText) != "") {
                    this.showGender = true;
                }
            },
            isMale: function () {
                if ($.trim(this.value.NameText) != "") {
                    this.value.Gender = 0;
                    this.value.Name = $.trim(this.value.NameText);
                    this.value.NameText = this.maleText;
                }
                else {
                    this.value.Name = "";
                    this.value.NameText = "";
                }
                this.showGender = false;
            },
            isFemale: function () {
                if ($.trim(this.value.NameText) != "") {
                    this.value.Gender = 1;
                    this.value.Name = $.trim(this.value.NameText);
                    this.value.NameText = this.femaleText;
                }
                else {
                    this.value.Name = "";
                    this.value.NameText = "";
                }
                this.showGender = false;
            },
            setCurrentValue(value) {
                if (value.Mobile) {
                    value.Mobile.push({
                        Type: 0,
                        TypeText: "手机",
                        IsUsually: false,
                        ContactValue: "",
                    });
                } else {
                    value.Mobile = [{
                        Type: 0,
                        TypeText: "手机",
                        IsUsually: true,
                        ContactValue: "",
                    }];
                }

                if (!value.Telephone) {
                    value.Telephone = {
                        Type: 1,
                        TypeText: "座机",
                        IsUsually: true,
                        ContactValue: "",
                    };
                }
                if (!value.WeChat) {
                    value.WeChat = {
                        Type: 2,
                        TypeText: "微信",
                        IsUsually: true,
                        ContactValue: "",
                    };
                }
                if (!value.QQ) {
                    value.QQ = {
                        Type: 3,
                        TypeText: "QQ",
                        IsUsually: true,
                        ContactValue: "",
                    };
                }
                return value;
            },
            setParentValue() {
                this.value.Contacts = [];
                for (var i = 0; i < this.value.Mobile.length; i++) {
                    if (this.value.Mobile[i].ContactValue != "") {
                        this.value.Contacts.push({
                            Type: 0,
                            TypeText: "手机",
                            ContactValue: this.value.Mobile[i].ContactValue,
                            IsUsually: this.value.Mobile[i].IsUsually,
                        });
                    }
                }
                if ($.trim(this.value.Telephone.ContactValue) != "") {
                    this.value.Contacts.push({
                        Type: 1,
                        TypeText: "座机",
                        ContactValue: $.trim(this.value.Telephone.ContactValue),
                        IsUsually: true,
                    });
                }
                if ($.trim(this.value.WeChat.ContactValue) != "") {
                    this.value.Contacts.push({
                        Type: 2,
                        TypeText: "微信",
                        ContactValue: $.trim(this.value.WeChat.ContactValue),
                        IsUsually: true,
                    });
                }
                if ($.trim(this.value.QQ.ContactValue) != "") {
                    this.value.Contacts.push({
                        Type: 3,
                        TypeText: "QQ",
                        ContactValue: $.trim(this.value.QQ.ContactValue),
                        IsUsually: true,
                    });
                }
                this.$emit('input', this.value);
            },
            //watch: {
            //    tempMobile: function (val) {
            //        this.setParentValue();
            //    }
            //},
        }
    };
    return ContactEdit;
});
